<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Http\Models\Post;
use App\Http\Models\PostCid;

class PostController extends Controller {


    public function index(Request $request)
    {
        //data不可数
        $data = Post::orderBy("created_at","desc");
        $data = $data->paginate(20);

        $cids = PostCid::all();

        return view("admin.post.index",['data'=>$data,'cids'=>$cids,'active'=>'post']);
    }

    public function create(Request $request)
    {
        $cids = PostCid::all();

        return view('admin.post.create',['cids'=>$cids,'active'=>'post']);
    }

    public function store(Request $request){
        $post = new Post;
        $post -> title = $request->input('title');
        $post -> cid = $request -> input('cid');//文章的分类
        $post -> content = $request -> input('content');
        $post -> save();
        return redirect('/administrator/post');
    }

    public function edit($id){

        $post = Post::find($id);

        $cids = PostCid::all();

        return view('admin.post.edit',['post'=>$post,'cids'=>$cids]);
    }


    public function update(Request $request,$id)
    {
        $post = Post::find($id);

        $post ->title = $request -> input('title');
        $post ->content = $request -> input('content');
        $post ->cid = $request -> input('cid');

        $post->save();

        return redirect('/administrator/post');
    }

    public function delete($id)
    {
        $res = Post::destroy($id);
        if($res) return view('success',['url'=>'/administrator/post']);

        return view('failure',['url'=>'/administrator/post']);
    }


}
